package org.jeecg.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.SpringProperties;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;

@Component
public class StartedAppListener implements CommandLineRunner {

	private static final Logger LG = LoggerFactory.getLogger(StartedAppListener.class);

	@Autowired
	private ApplicationContext applicationContext;

	@Override
	public void run(String... args) throws Exception {
		// 系统启动完成
		for (String arg : args) {
			LG.info(arg);
		}

		// 忽略参数为空，包空指针问题
		SpringProperties.setProperty("spring.jdbc.getParameterType.ignore", "true");
		COSMOS.SPRING_CONTEXT = applicationContext;
		// 加载其他模块初始化工作
		Map<String, AccessPoint> accessPointMap = applicationContext.getBeansOfType(AccessPoint.class);
		Iterator<AccessPoint> iterator = accessPointMap.values().iterator();
		while(iterator.hasNext()){
			AccessPoint accessPoint = iterator.next();
			accessPoint.completed(args);
		}
	}

	/**
	 * 启动完成接入点
	 */
	public static interface AccessPoint{
		// 完成
		public void completed(String... args) throws Exception;
	}
}
